package com.example.lenovo.myapplication;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class   MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageButton mbtnWechat;
    private ImageButton mbtnTxl;
    private ImageButton mbtnFind;
    private ImageButton mbtnMe;
    private List<Msg> msgList = new ArrayList<>();
    private EditText inputText;
    private Button send;
    private RecyclerView msgRecyclerView;
    private MsgAdapter adapter;
  //  private WxFragment mWxFragment = null;
    private TxlFragment mTxlFragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout2);
        initMsgs();//初始化消息数据
        inputText = (EditText) findViewById(R.id.input_text);
        send = (Button)findViewById(R.id.send);
        msgRecyclerView = (RecyclerView)findViewById(R.id.msg_recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        msgRecyclerView.setLayoutManager(layoutManager);
        adapter = new MsgAdapter(msgList);
        msgRecyclerView.setAdapter(adapter);
        mbtnWechat = findViewById(R.id.wx);
        mbtnTxl = findViewById(R.id.txl);
        mbtnFind = findViewById(R.id.fx);
        mbtnMe = findViewById(R.id.wo);

        mbtnMe.setOnClickListener(this);
        mbtnWechat.setOnClickListener(this);
        mbtnFind.setOnClickListener(this);
        mbtnTxl.setOnClickListener(this);

        selectTab(0);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = inputText.getText().toString();
                if(!"".equals(content)){
                    Msg msg = new Msg(content,Msg.TYPE_SENT);
                    msgList.add(msg);
                    adapter.notifyItemInserted(msgList.size() - 1);
                    //将msgRecyclerView定位到最后一行
                    inputText.setText("");//清空内容

                }
            }
        });

    }

    private void initMsgs() {
        Msg msg1 = new Msg("Hello guy",Msg.TYPE_RECEIVED);
        msgList.add(msg1);
        Msg msg2 = new Msg("Hellom who is that",Msg.TYPE_SENT);
        msgList.add(msg2);
        Msg msg3 = new Msg("This is Tom. Nice to talking to you",Msg.TYPE_RECEIVED);
        msgList.add(msg3);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.wx:
                selectTab(0);
                break;
            case R.id.txl:
                selectTab(1);
                break;
            case R.id.fx:
                selectTab(2);
                break;
            case R.id.wo:
                selectTab(3);
                break;
        }
    }

    private void selectTab(int i) {
        setTabButtonDefaultColor();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragments(transaction);
        switch (i) {
            case 0:
                mbtnWechat.setImageResource(R.mipmap.tab_weixin_pressed);
                /*if(mWxFragment == null){
                    mWxFragment = new WxFragment();
                    transaction.add(R.id.f1_content,mWxFragment);
                }
                else {
                    transaction.show(mWxFragment);
                }*/
                break;
            case 1:
                mbtnTxl.setImageResource(R.mipmap.tab_address_pressed);
                if(mTxlFragment == null){
                    mTxlFragment = new TxlFragment();
                    transaction.add(R.id.f1_content,mTxlFragment);

                }
                else{
                    transaction.show(mTxlFragment);
                }
                break;
            case 2:
                mbtnFind.setImageResource(R.mipmap.tab_find_frd_pressed);
                break;
            case 3:
                mbtnMe.setImageResource(R.mipmap.tab_settings_pressed);
                break;
        }
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
       /* if (mWxFragment != null)
        {
            transaction.hide(mWxFragment);
        }*/

        if (mTxlFragment != null) {
            transaction.hide(mTxlFragment);
        }
    }

    private void setTabButtonDefaultColor() {
        mbtnWechat.setImageResource(R.mipmap.tab_weixin_normal);
        mbtnTxl.setImageResource(R.mipmap.tab_address_normal);
        mbtnFind.setImageResource(R.mipmap.tab_find_frd_normal);
        mbtnMe.setImageResource(R.mipmap.tab_settings_normal);
    }
}
